@echo off

rem *************************************************************************
rem ** Directories
rem *************************************************************************
set GNU_VERSION=3.4.3
set SDK30_DIR=D:\Programming\SAGEM\SDK30
set SDK30_VER=TDS 5.5
set SDK30_SDK_DIR=%SDK30_DIR%\%SDK30_VER%\SDK
set GNU_DIR=D:\Programming\SAGEM\GCC_%GNU_VERSION%

set EMV_INC_DIR=..\Inc
set EMV_CUSTOM_SRC_DIR=..\Src
set EMV_LIB_ADS_DIR=..\lib_rvd
set EMV_LIB_GNU_DIR=..\lib_gnu
set EMV_LIB_SDT_DIR=..\lib_sdt
set EMV_LIB_VC6_DIR=..\lib_vc6
set EMV_LIB_VS2005_DIR=\lib_vs2005
set GLOB_INTER=..\..\inc
set OBJ_DIR=Obj

set SCHEMES_DIR=%SDK30_DIR%\%SDK30_VER%\Component\Schemes\B1
set OUTPUT_DIR=%CD%\Signed



rem *************************************************************************
rem Application settings
rem *************************************************************************
set APP_NAME=MASTER
set APP_VERSION=35220325

set OUTPUT_DATA_SIZE=00080000
set OUTPUT_APPLI_TYPE=0051

set SST_CERTIFICATE_FILE=certif-0002A772-0110-70.V03
rem set SST_CERTIFICATE_FILE=Mockup.V03
set SST_PIN=32826128
rem set SST_PIN=00000000

set NUMBER_OF_SCHEMES=4
set SCHEME1=SchFree.mot
set SCHEME2=SchGetPin.bin
set SCHEME3=SchIso9564.mot
set SCHEME4=SchLoadData.mot

set CATALOG_FILE=31
set OUTPUT_ZIPPED=1
set OUTPUT_CHECK_CRC=1
set OUTPUT_COMMENT=Master App
set OUTPUT_FILE_NAME=%APP_VERSION%.SGN
set OUTPUT_FAMILY_NAME=%APP_VERSION%

rem *************************************************************************
rem ** Compiler options
rem *************************************************************************
set CC_DEFINES=-D_USE_DEL_LIB_ -D_SECDEL_ -D_EXPORT_ -D_EFT30_ -D_POSIX_SOURCE
set CC_INCLUDES_DIR=-I"%EMV_INC_DIR%" -I"%SDK30_SDK_DIR%\inc" -I"%SDK30_SDK_DIR%\ISO_inc" -I"%GLOB_INTER%"

set CC_FLAGS=-g -Os -Wall -Wfloat-equal -Wcast-align -Wdisabled-optimization -funsigned-char -fshort-enums -mthumb -mthumb-interwork -mcpu=arm920t -msoft-float -mstructure-size-boundary=8 -c -pipe -fmessage-length=0 -ftabstop=4
set CC_ERROR=0

rem *************************************************************************
rem ** Linker options
rem *************************************************************************
set LD_OBJECTS="%OBJ_DIR%\_emvdc_.o" "%OBJ_DIR%\Entry.o" "%OBJ_DIR%\Main.o" "%OBJ_DIR%\fm_genpinpad.o" "%OBJ_DIR%\fm_UserInterface.o"
set LD_LIBS="%EMV_LIB_GNU_DIR%\DEL.lib"

set AXF_FILE=%OBJ_DIR%\%APP_NAME%.axf
set MAP_FILE=%OBJ_DIR%\%APP_NAME%.map
set BIN_FILE_NAME=%APP_NAME%.bin
set TXT_FILE_NAME=%APP_NAME%.txt
set BIN_FILE=%OBJ_DIR%\%BIN_FILE_NAME%
set TXT_FILE=%OBJ_DIR%\%TXT_FILE_NAME%

set LD_FLAGS=-O3 --cref -Map "%MAP_FILE%" -T "%SDK30_SDK_DIR%\lib_gnu\gcc_link.txt"
set LD_SYS_OBJECTS="%SDK30_SDK_DIR%\lib_gnu\cstartup.o"
set LD_SYS_LIBS="%SDK30_SDK_DIR%\lib_gnu\eft30.lib" "%SDK30_SDK_DIR%\lib_gnu\PinLib.lib" "%SDK30_SDK_DIR%\lib_gnu\IAPP.lib" "%SDK30_SDK_DIR%\lib_gnu\trap.lib"
set LD_GNU_LIBS=-L"%GNU_DIR%\arm-elf\lib\thumb\soft\interwork" -L"%GNU_DIR%\lib\gcc\arm-elf\%GNU_VERSION%\thumb\soft\interwork" -lc -lgcc
set LD_ERROR=0
set FROMELF_ERROR=0

rem *************************************************************************
rem ** SST options
rem *************************************************************************
set SST_DIR=D:\Programming\SAGEM\TELIUM Tools\SST30 V04.02
set SST_CONFIG_FILE=%SST_DIR%\%APP_NAME%.prm

rem *************************************************************************
rem ** Create output directories
rem *************************************************************************
set ERRORS_FILE=Errors.txt

del "%ERRORS_FILE%" > nul 2> nul

rem rd /s /q "%OBJ_DIR%" > nul 2> nul
rem md "%OBJ_DIR%"
rem 2>> "%ERRORS_FILE%"
del /Q "%OBJ_DIR%"\*.* > nul 2> nul

rem rd /s /q "%OUTPUT_DIR%" > nul 2> nul
rem md "%OUTPUT_DIR%"
rem 2>> "%ERRORS_FILE%"
del /Q "%OUTPUT_DIR%"\*.* > nul 2> nul

echo 1,%OUTPUT_FILE_NAME%,%OUTPUT_APPLI_TYPE%,%OUTPUT_FAMILY_NAME%,%OUTPUT_ZIPPED%,%OUTPUT_CHECK_CRC%,0000,00000000,00000000,%OUTPUT_DATA_SIZE%,00000000,00200000,%OUTPUT_COMMENT%> "%TXT_FILE%"


rem *************************************************************************
rem ** Compile files
rem *************************************************************************
echo Compiling...
echo Compiling... >> "%ERRORS_FILE%"
call :CallCompiler %EMV_CUSTOM_SRC_DIR% _emvdc_
call :CallCompiler %EMV_CUSTOM_SRC_DIR% Entry
call :CallCompiler %EMV_CUSTOM_SRC_DIR% Main
call :CallCompiler %EMV_CUSTOM_SRC_DIR% fm_genpinpad
call :CallCompiler %EMV_CUSTOM_SRC_DIR% fm_UserInterface



if .%CC_ERROR%.==.0. goto AfterCompile
echo.
echo.
echo *************************************
echo * THERE ARE COMPILATION ERRORS !!!! *
echo *************************************
pause
goto End

:AfterCompile

rem *************************************************************************
rem ** Link files
rem *************************************************************************
echo Linking...
echo Linking... >> "%ERRORS_FILE%"
call :CallLinker
if .%LD_ERROR%.==.0. goto AfterLink
echo.
echo.
echo ********************************
echo * THERE ARE LINKER ERRORS !!!! *
echo ********************************
pause
goto End

:AfterLink

rem *************************************************************************
rem ** Generate binary file
rem *************************************************************************
echo Generating binary file...
echo Generating binary file... >> "%ERRORS_FILE%"
call :CallFromElf
if .%FROMELF_ERROR%.==.0. goto AfterFromElf
echo.
echo.
echo ************************************************
echo * THERE ARE BINARY FILE GENERATION ERRORS !!!! *
echo ************************************************
pause
goto End

:AfterFromElf
rem *************************************************************************
rem ** Sign the application
rem *************************************************************************
echo Signing...
echo Signing... >> "%ERRORS_FILE%"
call :CallSST

rem *************************************************************************
rem ** End
rem *************************************************************************
:End
rem start %ERRORS_FILE%
goto Exit

rem *************************************************************************
rem ** Call the compiler
rem **  %1 is the directory
rem **  %2 is the file name without extention
rem *************************************************************************
:CallCompiler
echo %1\%2.c
echo %1\%2.c >> %ERRORS_FILE%
"%GNU_DIR%\bin\arm-elf-gcc" %CC_FLAGS% %CC_INCLUDES_DIR% %CC_DEFINES% "%1\%2.c" -o "%OBJ_DIR%\%2.o"
rem 2>> "%ERRORS_FILE%"
if not ERRORLEVEL 1 goto Exit
set CC_ERROR=1
goto Exit

rem *************************************************************************
rem ** Call the linker
rem *************************************************************************
:CallLinker
"%GNU_DIR%\bin\arm-elf-ld" %LD_FLAGS% %LD_OBJECTS% %LD_SYS_OBJECTS% %LD_LIBS% %LD_SYS_LIBS% %LD_GNU_LIBS% -o "%AXF_FILE%"
rem 2>> "%ERRORS_FILE%"
if ERRORLEVEL 1 goto LinkError
echo.>> "%MAP_FILE%"
echo.>> "%MAP_FILE%"
echo Sizes (total data = data + bss)>> "%MAP_FILE%"
echo ===============================>> "%MAP_FILE%"
"%GNU_DIR%\bin\arm-elf-size" "%AXF_FILE%" >> "%MAP_FILE%"
goto Exit

:LinkError
set LD_ERROR=1
goto Exit

rem *************************************************************************
rem ** Call fromelf to generate the binary file
rem *************************************************************************
:CallFromElf
"%GNU_DIR%\bin\arm-elf-objcopy" --output-target=binary "%AXF_FILE%" "%BIN_FILE%"
rem 2>> "%ERRORS_FILE%"
if not ERRORLEVEL 1 goto Exit
set FROMELF_ERROR=1
goto Exit

rem *************************************************************************
rem ** Sign the application
rem *************************************************************************
:CallSST
del "%SST_CONFIG_FILE%" > NUL 2> NUL

echo [SST]>> "%SST_CONFIG_FILE%"
echo ReaderName=>> "%SST_CONFIG_FILE%"
echo CertificateSchemaDir=%SST_DIR%\certificate>> "%SST_CONFIG_FILE%"
echo CertificateAppliDir=%SST_DIR%\certificate>> "%SST_CONFIG_FILE%"
echo SchemaDir=%SCHEMES_DIR%>> "%SST_CONFIG_FILE%"
echo SignedSchemaDir=%CD%\%OBJ_DIR%>> "%SST_CONFIG_FILE%"
echo ParametersDir=%CD%\%OBJ_DIR%>> "%SST_CONFIG_FILE%"
echo DestinationDir=%OUTPUT_DIR%>> "%SST_CONFIG_FILE%"
echo ApplicationDir=%CD%\%OBJ_DIR%>> "%SST_CONFIG_FILE%"
echo TempDir=%SST_DIR%\Temp>> "%SST_CONFIG_FILE%"
echo.>> "%SST_CONFIG_FILE%"

echo [Schemas]>> "%SST_CONFIG_FILE%"
echo NbSchemas=%NUMBER_OF_SCHEMES% >> "%SST_CONFIG_FILE%"
echo CertificateFile=%SST_CERTIFICATE_FILE%>> "%SST_CONFIG_FILE%"
echo Schema1=%SCHEME1%>> "%SST_CONFIG_FILE%"
echo Schema2=%SCHEME2%>> "%SST_CONFIG_FILE%"
echo Schema3=%SCHEME3%>> "%SST_CONFIG_FILE%"
echo Schema4=%SCHEME4%>> "%SST_CONFIG_FILE%"
echo Schema5=%SCHEME5%>> "%SST_CONFIG_FILE%"
echo Schema6=%SCHEME6%>> "%SST_CONFIG_FILE%"
echo Schema7=%SCHEME7%>> "%SST_CONFIG_FILE%"
echo Schema8=%SCHEME8%>> "%SST_CONFIG_FILE%"
echo Schema9=%SCHEME9%>> "%SST_CONFIG_FILE%"
echo Schema10=%SCHEME10%>> "%SST_CONFIG_FILE%"
echo.>> "%SST_CONFIG_FILE%"

echo [Application]>> "%SST_CONFIG_FILE%"
echo ParameterFile=%TXT_FILE_NAME%>> "%SST_CONFIG_FILE%"
echo CertificateFile=%SST_CERTIFICATE_FILE%>> "%SST_CONFIG_FILE%"
echo ApplicationFile=%BIN_FILE_NAME%>> "%SST_CONFIG_FILE%"
echo CatalogFileExtension=%CATALOG_FILE%>> "%SST_CONFIG_FILE%"

"%SST_DIR%\SST30.exe" 2,%SST_CONFIG_FILE%,%SST_PIN%> NUL 2> NUL
rem del "%SST_CONFIG_FILE%" > NUL 2> NUL

goto Exit

rem *************************************************************************
rem ** Exit
rem *************************************************************************
:Exit
